// 本地数据存储封装

// 设置存储
function set(key, value) {
  try {
    wx.setStorageSync(key, value)
    return true
  } catch (e) {
    console.error('存储失败', e)
    return false
  }
}

// 获取存储
function get(key, defaultValue = null) {
  try {
    const value = wx.getStorageSync(key)
    return value !== '' ? value : defaultValue
  } catch (e) {
    console.error('获取存储失败', e)
    return defaultValue
  }
}

// 删除存储
function remove(key) {
  try {
    wx.removeStorageSync(key)
    return true
  } catch (e) {
    console.error('删除存储失败', e)
    return false
  }
}

// 清空所有存储
function clear() {
  try {
    wx.clearStorageSync()
    return true
  } catch (e) {
    console.error('清空存储失败', e)
    return false
  }
}

// 批量设置存储
function setBatch(data) {
  try {
    for (let key in data) {
      wx.setStorageSync(key, data[key])
    }
    return true
  } catch (e) {
    console.error('批量存储失败', e)
    return false
  }
}

// 批量获取存储
function getBatch(keys) {
  try {
    const result = {}
    keys.forEach(key => {
      result[key] = wx.getStorageSync(key)
    })
    return result
  } catch (e) {
    console.error('批量获取存储失败', e)
    return {}
  }
}

module.exports = {
  set,
  get,
  remove,
  clear,
  setBatch,
  getBatch
}